package com.digitalchina.lxnsy.view.ChannelView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.RealTimeMarketActivity;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.entity.PriceTabVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.PriceTabHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.digitalchina.lxnsy.constant.MSG.MSG_GET_PRODUCE_PRICE_STATISTICS_LIST_FIELD;
import static com.digitalchina.lxnsy.constant.MSG.MSG_GET_PRODUCE_PRICE_STATISTICS_LIST_SUCCESS;

/**
 * ================================================
 * 项目名称：TabDemo
 * 类描述：
 * 包名：com.digitalchina.tabdemo
 * 创建人：郑皓
 * 创建时间：2017/7/17 21:24
 * 修改备注：
 * ================================================
 */
public class PriceFragment extends BaseV4Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private RecyclerArrayAdapter adapter;
    private List<PriceTabVO> priceTabVOList = new ArrayList<>();

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private String code = "";

    CallBackValue callBackValue;

    @Override
    public View setRootView() {

        return inflate(R.layout.fragment_price);
    }

    /**
     * fragment与activity产生关联  回调这个方法
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue = (CallBackValue) getActivity();
    }


    @Override
    public void initMember() {
        if (getArguments() != null) {
            code = getArguments().getString("code");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new PriceTabHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1.0f, 10.0f, 10.0f));

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                GotoUtil.gotoActivity(getActivity(), RealTimeMarketActivity.class, "PriceTabVO", adapter.getItem(position));
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_GET_PRODUCE_PRICE_STATISTICS_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG_GET_PRODUCE_PRICE_STATISTICS_LIST_FIELD:

                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        priceTabVOList.clear();
        priceTabVOList = FastJsonUtil.getListBean(object.toString(), "priceList", PriceTabVO.class);

        if (priceTabVOList != null) {
            adapter.clear();
            adapter.addAll(priceTabVOList);
//            if (priceTabVOList.size() > 0)
//                callBackValue.SendMessageValue(priceTabVOList.get(0).getCollectDate() + "  更新");
        }


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onRefresh() {
//        BusinessRealTimePrice.getProducePriceStatisticsList(getActivity(), code, mHandler);
    }

    //定义一个回调接口
    public interface CallBackValue {
        public void SendMessageValue(String strValue);
    }

}